<?php

class Controller_Admin_Cart extends Controller_Admin_Template
{
	protected $template = 'admin/template';
	private $db;

	public function before()
	{
		parent::before();
		$this->model = Model::factory('cart');
	}

	public function actionOrders()
	{
		$this->template->title = 'Manage orders';
		$this->template->content = new View('admin/orders');
		$this->template->content->orders = $this->model->getOrders();

	}

	public function actionOrderDetails($orderId)
	{
		$this->template->title = 'Manage orders';
		$this->template->content = new View('admin/orderDetails');
		$this->template->content->order = $this->model->getOrder($orderId);
		$this->template->content->items = $this->model->getOrderItems($orderId);
	}

	public function actionPaid($orderId)
	{
		if (($order = $this->model->getOrder($orderId))) {
			Cart::instance()->setPaid($orderId);
		}
		$this->response->redirect(Request::$controller . 'orders/');
	}

	public function actionUnpaid($orderId)
	{
		if (($order = $this->model->getOrder($orderId))) {
			Model_Table::instance('cartOrders')->update(array('isPaid' => 0), $orderId);
		}
		$this->response->redirect(Request::$controller . 'orders/');
	}

	public function actionProcessed($orderId)
	{
		if (($order = $this->model->getOrder($orderId))) {
			Model_Table::instance('cartOrders')->update(array('processed' => 1), $orderId);
		}
		$this->response->redirect(Request::$controller . 'orders/');
	}

	public function actionUnprocessed($orderId)
	{
		if (($order = $this->model->getOrder($orderId))) {
			Model_Table::instance('cartOrders')->update(array('processed' => 0), $orderId);
		}
		$this->response->redirect(Request::$controller . 'orders/');
	}


	public function actionRemove($orderId)
	{
		if (($order = $this->model->getOrder($orderId))) {
			Database::instance()->query('DELETE FROM `cartItems` WHERE `orderId` = ?', $orderId);
			Model_Table::instance('cartOrders')->delete((int)$order['id']);
		}
		$this->response->redirect(Request::$controller . 'orders/');
	}
}
